package custom.base.ui.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cubebox on 2018/3/30.
 */
public class DecorationSpaceItem extends RecyclerView.ItemDecoration {
    int mSpace;
    private int startSpace = 0, endSpace = 0;
    private int mOrientation;
    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
    private List<Integer> ignorePositionList = new ArrayList<>();

    /**
     * @param space 间距大小
     *              默认头尾无间距
     */
    public DecorationSpaceItem(int orientation, int space) {
        this(orientation, space, 0, 0);
    }

    /**
     * @param space      间距大小
     * @param endSpace   结束间距
     * @param startSpace 开始间距
     */
    public DecorationSpaceItem(int orientation, int space, int startSpace, int endSpace, int... ignorePosition) {
        this.startSpace = startSpace;
        this.endSpace = endSpace;
        this.mSpace = space;
        setOrientation(orientation);
        for (int i = 0; i < ignorePosition.length; i++) {
            ignorePositionList.add(ignorePosition[i]);
        }
    }

    //设置屏幕的方向
    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        final int lastPosition = state.getItemCount() - 1;//整个RecyclerView最后一个item的position
        final int current = parent.getChildLayoutPosition(view);//获取当前要进行布局的item的position
        if (!ignorePositionList.contains(current)) {
            switch (mOrientation) {
                case HORIZONTAL_LIST:
                    if (current == 0) {
                        outRect.left = startSpace;
                    } else if (current == lastPosition) {
                        outRect.left = mSpace;
                        outRect.right = endSpace;
                    } else {
                        outRect.left = mSpace;
                    }
                    break;
                case VERTICAL_LIST:
                    if (current == 0) {
                        outRect.top = startSpace;
                    } else if (current == lastPosition) {
                        outRect.top = mSpace;
                        outRect.bottom = endSpace;
                    } else {
                        outRect.top = mSpace;
                    }
                    break;
            }
        } else {
            outRect.left = 0;
            outRect.top = 0;
            outRect.right = 0;
            outRect.bottom = 0;
        }

    }

}
